using System;
using System.Collections;

using Microsoft.MediaCenter.UI;

using GalleryItem = McePicasa.GalleryItem;

namespace McePicasa
{
    /// <summary>
    /// This object contains the standard set of information displayed in the 
    /// details page UI.
    /// </summary>
    public class DetailsPage : ModelItem
    {
        public DetailsPage()
        {
        }

        public DetailsPage(GalleryItem item, bool isOnSlideshow)
        {
            gallItem = item;
            this.Owner = item;
            IsOnSlideshow = isOnSlideshow;
            activePage = this;
        }
        public DetailsPage(GalleryItemFlickr item, bool isOnSlideshow)
        {
            //gallItem = item;
            this.Owner = item;
            IsOnSlideshow = isOnSlideshow;
            activePage = this;
        }

        /// <summary>
        /// The primary title of the object.
        /// </summary>
        public string Title
        {
            get { return title; }
            set
            {
                if (title != value)
                {
                    title = value;
                    FirePropertyChanged("Title");
                }
            }
        }

        /// <summary>
        /// A multiline summary of the object.
        /// </summary>
        public string Summary
        {
            get { return summary; }
            set
            {
                if (summary != value)
                {
                    summary = value;
                    FirePropertyChanged("Summary");
                }
            }
        }

        /// <summary>
        /// A list of actions that can be performed on this object.
        /// This list should only contain objects of type Command.
        /// </summary>
        public IList Commands
        {
            get { return commands; }
        }

        public void SetCommands(IList value)
        {
            if (commands != value)
            {
                commands = value;
                FirePropertyChanged("Commands");
            }
        }

        /// <summary>
        /// Additional minor metadata about this object.
        /// </summary>
        public string Metadata
        {
            get { return metadata; }
            set
            {
                if (metadata != value)
                {
                    metadata = value;
                    FirePropertyChanged("Metadata");
                }
            }
        }

        /// <summary>
        /// A fullscreen image to display in the background.
        /// </summary>
        public Image Background
        {
            get { return backgroundImage; }
            set
            {
                if (backgroundImage != value)
                {
                    backgroundImage = value;
                    FirePropertyChanged("Background");
                }
            }
        }

        public Command NextCmd
        {
            get { return nextCmd; }
            set { nextCmd = value; FirePropertyChanged("nextCmd"); }
        }


        public Command PrevCmd
        {
            get { return prevCmd; }
            set { prevCmd = value; FirePropertyChanged("prevCmd"); }
        }

        public GalleryItem GallItem
        {
            get { return gallItem; }
            set { gallItem = value; }
        }

        public void GoUp()
        {
            if (isOnSlideshow)
            {
                CancelSlideshow();
            }

            if (gallItem == null)
                Application.Current.MessageBox("gallItem == null", "GoUp");
            else if (gallItem.Owner == null)
                Application.Current.MessageBox("gallItem.Owner == null", "GoUp");
            else if (gallItem.Owner is GalleryPage)
                Application.Current.GoToGallery(gallItem.Owner as GalleryPage);
            else if (gallItem.Owner is SearchPage)
                Application.Current.GoToSearch(gallItem.Owner as SearchPage);
            else
                Application.Current.MessageBox("gallItem.Owner is Unknown!", "GoUp");
        }

        public string DetailsPageStatusText
        {
            get { return detailsPageStatusText; }
            set { detailsPageStatusText = value; FirePropertyChanged("DetailsPageStatusText"); }
        }

        public bool IsOnSlideshow
        {
            get { return isOnSlideshow; }
            set { isOnSlideshow = value; }
        }
                
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                if (SlideshowTimer != null)
                {
                    SlideshowTimer.Stop();
                    SlideshowTimer.Dispose();
                    SlideshowTimer = null;
                }
            }
            // free native resources
        }

        private string title;
        private string summary;
        private IList commands;
        private string metadata;
        private Image backgroundImage;
        private Command nextCmd;
        private Command prevCmd;
        private GalleryItem gallItem;
        private string detailsPageStatusText;
        private bool isOnSlideshow;

        #region Slideshow

        /// <summary>
        /// This method is to control play/pause status when user clicks on
        /// the Play/Pause button of the Slideshow page.
        /// </summary>
        public static void PlayPause()
        {
            if (DetailsPage.IsPlayed)
            {
                SlideshowTimer.Stop();
                PlayPauseText = " Play  ";
            }
            else
            {
                SlideshowTimer.Start();
                PlayPauseText = "Pause";
            }
            DetailsPage.IsPlayed = (DetailsPage.IsPlayed == true ? false : true);
        }

        /// <summary>
        /// This method is to cancel slide show when user clicks on the GoUp
        /// button of the Slideshow page.
        /// </summary>
        public static void CancelSlideshow()
        {
            SlideshowTimer.Stop();
            SlideshowTimer.Dispose();
            SlideshowTimer = null;
            IsPlayed = false;
            activePage.IsOnSlideshow = false;
        }

        public static Timer SlideshowTimer
        {
            get { return DetailsPage.slideshowTimer; }
            set { DetailsPage.slideshowTimer = value; }
        }

        public static bool IsPlayed
        {
            get { return DetailsPage.isPlayed; }
            set { DetailsPage.isPlayed = value; }
        }

        public static string PlayPauseText
        {
            get { return playPauseText; }
            set { playPauseText = value; activePage.FirePropertyChanged("PlayPauseText"); }
        }

        private static Timer slideshowTimer;
        private static bool isPlayed;
        private static string playPauseText = "Pause";
        private static DetailsPage activePage;

        #endregion
    }
}
